package com.bst.bsj.portal.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.homewaimai.common.lang.FormaterParseUtil;

public class CalendarUtil {

    // 最大失效日期常量
    public static Date MAX_END_DATE = FormaterParseUtil.parseEnDate("3000-01-01", "yyyy-MM-dd");

    /**
     * Adds the given number of days to a date.
     * 
     * @param date the date
     * @param days number of days
     */
    public static void addDaysToDate(Date date, int days) {
        date.setDate(date.getDate() + days);
    }

    /**
     * Adds the given number of months to a date.
     * 
     * @param date the date
     * @param months number of months
     */
    public static void addMonthsToDate(Date date, int months) {
        if (months != 0) {
            int month = date.getMonth();
            int year = date.getYear();

            int resultMonthCount = year * 12 + month + months;
            int resultYear = resultMonthCount / 12;
            int resultMonth = resultMonthCount - resultYear * 12;

            date.setMonth(resultMonth);
            date.setYear(resultYear);
        }
    }

    /**
     * Copies a date.
     * 
     * @param date the date
     * @return the copy
     */
    public static Date copyDate(Date date) {
        if (date == null) {
            return null;
        }
        Date newDate = new Date();
        newDate.setTime(date.getTime());
        return newDate;
    }

    /**
     * Returns the number of days between the two dates. Time is ignored.
     * 
     * @param start starting date
     * @param finish ending date
     * @return the different
     */
    public static int getDaysBetween(Date start, Date finish) {
        if (hasTime(start)) {
            start = copyDate(start);
            resetTime(start);
        }

        if (hasTime(finish)) {
            finish = copyDate(finish);
            resetTime(finish);
        }

        long aTime = start.getTime();
        long bTime = finish.getTime();

        long adjust = 60 * 60 * 1000;
        adjust = (bTime > aTime) ? adjust : -adjust;

        return (int) ((bTime - aTime + adjust) / (24 * 60 * 60 * 1000));
    }

    /**
     * Resets the date to have no time modifiers.
     * 
     * @param date the date
     */
    public static void resetTime(Date date) {
        long msec = date.getTime();
        msec = (msec / 1000) * 1000;
        date.setTime(msec);

        date.setHours(0);
        date.setMinutes(0);
        date.setSeconds(0);
    }

    /**
     * Sets a date object to be at the beginning of the month and no time specified.
     * 
     * @param date the date
     */
    public static void setToFirstDayOfMonth(Date date) {
        resetTime(date);
        date.setDate(1);
    }

    public static String getPrettyTime(Date oldDate) {
        long time = new Date().getTime() - oldDate.getTime();// time 单位是 毫秒
        String res = "";
        // 先判断是不是小于 60 * 60 * 1000 也就是 小于1小时，那么显示 ： **分钟前
        if (time >= 24 * 60 * 60 * 1000) { // 如果大于等于1小时 小于等于一天，那么显示 ： **小时前
            return (time / 1000 / 60 / 60 / 24) + "天前";
        }

        if (time >= 60 * 60 * 1000 && time < 24 * 60 * 60 * 1000) { // // 如果大于等于1小时 小于等于一天，那么显示 ： **小时前
            return (time / 1000 / 60 / 60) + "小时前";
        }

        if (time < 60 * 60 * 1000) {
            return (time / 1000 / 60) + "分钟前";
        }
        return res;
    }

    public static int compareDate(Date date, String toCompare) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date toCompareDate = sdf.parse(toCompare);
            return date.compareTo(toCompareDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1;
    }

    private static boolean hasTime(Date start) {
        return start.getHours() != 0 || start.getMinutes() != 0 || start.getSeconds() != 0;
    }

    public static void main(String[] args) {
        Date date = new Date();
        CalendarUtil.addDaysToDate(date, -15);
        System.out.println(date);
    }
}
